import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;
import javax.swing.border.LineBorder;

//import Lot.PropertyListener;


public class PropertyLot extends Lot
{
	private int price;	// the price to purchase the property
	private Color color;		// the color of the PropertyLot
	private int rentValue;		// rent value with no upgrades
	private int rent1House;		// rent value with 1 house
	private int rent2Houses;	// rent value with 2 houses
	private int rent3Houses;	// rent value with 3 houses
	private int rent4Houses;	// rent value with 4 houses
	private int rentHotel;		// rent value with a hotel
	private int mortgageValue;	// value used to mortgage property
	private int houseCost;		// cost to buy 1 house
	private int hotelCost;		// cost to buy 1 hotel
	
	private int numHouses;		// from 0 to 4
	private int numHotels;		// from 0 to 4
	
	private int ownedBy;		// determines which players owns the property
								// 0 - no owner, 1 - P1 owns, 2 - P2 owns, etc.
	
//	private JLabel label1, label2; // for showing the names on the lots
	
	private ImageIcon logo; // image used for logo on the lot
	
	private PropertyCardPanel pcp;
//	private boolean mouseFlag;
	
	// Dimensions:
	private int WIDTH;			// width of the property graphic
	private int HEIGHT;			// height of the property graphic

	
	public PropertyLot(String name, String type, String location, int p, Color c,
					   int rV, int r1h, int r2h, int r3h, int r4h,
					   int rH, int mV, int houseC, int hotelC)
	{
		super(name, type, location);
				
		price = p;
		color = c;
		rentValue = rV;
		rent1House = r1h;
		rent2Houses = r2h;
		rent3Houses = r3h;
		rent4Houses = r4h;
		mortgageValue = mV;
		rentHotel = rH;
		houseCost = houseC;
		hotelCost = hotelC;
		
		numHouses = 0;
		numHotels = 0;
		ownedBy = 0; // initially no one owns it
		
//		mouseFlag = false;
		
		if(location == "north" || location == "south")
		{
			WIDTH = 55;
			HEIGHT = 100;
		}
		else if(location == "east" || location == "west")
		{
			WIDTH = 100;
			HEIGHT = 55;
		}
		
		//label1 = new JLabel(name);
		//add(label1);
		
		// sets the logo on the property on the game board
		setLogo();
		
		addMouseListener(new PropertyListener());
		pcp = new PropertyCardPanel();
		pcp.changeImage(name + ".jpg");
		pcp.setCostLabel(price);
		
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		setBorder(new LineBorder(Color.black));
		setBackground(Color.CYAN); // temporary to see the dimensions are correct
		setOpaque(false);
	} // end PropertyLot constructor
	
	public void setLogo()
	{
		// Creates a new ImageIcon from the name (String) of the property selected
		// and repaints??
		logo = new ImageIcon("./logos/" + name + "_small.jpg");
		//repaint(); // not needed???
	} // end setLogo
	
	public String getName()
	{
		return name;
	} // end getName
	
	public int getPrice()
	{
		return price;
	} // end getPrice

	public Color getColor()
	{
		return color;
	} // end getColor
	
	public int getRentValue()
	{
		return rentValue;
	} // end getRentValue
	
	public int getRent1House()
	{
		return rent1House;
	} // end getRent1House
	
	public int getRent2Houses()
	{
		return rent2Houses;
	} // end getRent2Houses
	
	public int getRent3Houses()
	{
		return rent3Houses;
	} // end getRent3Houses
	
	public int getRent4Houses()
	{
		return rent4Houses;
	} // end getRent4Houses
	
	public int getRentHotel()
	{
		return rentHotel;
	} // end getRentHotel
	
	public int getMortgageValue()
	{
		return mortgageValue;
	} // end getMortgageValue
	
	public int getHouseCost()
	{
		return houseCost;
	} // end getHouseCost
	
	public int getHotelCost()
	{
		return hotelCost;
	} // end getHotelCost
	
	public int getOwnedStatus()
	{
		return ownedBy;
	} // end getOwnedStatus
	
	public int getNumHouses()
	{
		return numHouses;
	}
	
	public void setNumHouses(int num)
	{
		numHouses = num;
		repaint();
	}

	public int getNumHotels()
	{
		return numHotels;
	}
	
	public void setNumHotels(int num)
	{
		numHotels = num;
		repaint();
	}

	
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		// fills the panel with the custom background board color
		g.setColor(new Color(200,222,186));
		
		if(location == "north" || location == "south")
		{
			g.fillRect(1, 1, 55, 100);
		}
		else if(location == "east" || location == "west")
		{
			g.fillRect(1, 1, 100, 55);
		}
		
		// fills a colored rectangle representing the color of the group
		// and paints the image of the property logo
		g.setColor(color);
		if(location == "north")
		{
			g.fillRect(0, 75, 55, 100);
			logo.paintIcon(this, g, 2, 1);
		}
		else if(location == "south")
		{
			g.fillRect(0, 0, 55, 25);
			logo.paintIcon(this, g, 1, 26);
		}
		else if(location == "east")
		{
			g.fillRect(0, 0, 25, 55);
			logo.paintIcon(this, g, 26, 1);
		}
		else if(location == "west")
		{
			g.fillRect(75, 0, 100, 55);
			//g.drawImage(logo.getImage(),0,0,null);
			logo.paintIcon(this, g, 1, 0);
		}
		
		
		// draw black lines to make property borders thicker looking
		g.setColor(Color.black);
		
		if(location == "north")
		{	
			g.drawLine(0, 74, 55, 74);
			g.drawLine(0, 75, 55, 75);
			g.drawLine(0, 98, 55, 98);
		}
		else if(location == "south")
		{
			g.drawLine(0, 24, 55, 24);
			g.drawLine(0, 25, 55, 25);
			g.drawLine(1, 1, 55, 1);
		}
		else if(location == "west")
		{
			g.drawLine(74, 0, 74, 55);
			g.drawLine(75, 0, 75, 55);
			g.drawLine(98, 1, 98, 55);
		}
		else if(location == "east")
		{
			g.drawLine(24, 1, 24, 55);
			g.drawLine(25, 1, 25, 55);
			g.drawLine(1, 1, 1, 55);
		}

		// draws houses or hotels if there are any
		if(location == "north")
		{
			if(numHotels == 1)
			{
				g.setColor(Color.black);
				g.drawRect(15, 77, 24, 19);
				g.setColor(Color.red);
				g.fillRect(16, 78, 23, 18);
			}
			else if(numHotels == 0)
			{
			
				if(numHouses >= 1)
				{
					g.setColor(Color.black);
					g.drawRect(41, 77, 11, 19);
					g.setColor(Color.green);
					g.fillRect(42, 78, 10, 18);
				}
				if(numHouses >= 2)
				{	
					g.setColor(Color.black);
					g.drawRect(28, 77, 11, 19);
					g.setColor(Color.green);
					g.fillRect(29, 78, 10, 18);
				}
				if(numHouses >= 3)
				{	
					g.setColor(Color.black);
					g.drawRect(15, 77, 11, 19);
					g.setColor(Color.green);
					g.fillRect(16, 78, 10, 18);
				}
				if(numHouses == 4)
				{
					g.setColor(Color.black);
					g.drawRect(2, 77, 11, 19);
					g.setColor(Color.green);
					g.fillRect(3, 78, 10, 18);
				}
			}
		}
		else if(location == "south")
		{
			if(numHotels == 1)
			{
				g.setColor(Color.black);
				g.drawRect(15, 3, 24, 19);
				g.setColor(Color.red);
				g.fillRect(16, 4, 23, 18);
			}
			else if(numHotels == 0)
			{
				if(numHouses >= 1)
				{
					g.setColor(Color.black);
					g.drawRect(2, 3, 11, 19);
					g.setColor(Color.green);
					g.fillRect(3, 4, 10, 18);
				}
				if(numHouses >= 2)
				{
					g.setColor(Color.black);
					g.drawRect(15, 3, 11, 19);
					g.setColor(Color.green);
					g.fillRect(16, 4, 10, 18);
				}
				if(numHouses >= 3)
				{
					g.setColor(Color.black);
					g.drawRect(28, 3, 11, 19);
					g.setColor(Color.green);
					g.fillRect(29, 4, 10, 18);
				}
				if(numHouses == 4)
				{
					g.setColor(Color.black);
					g.drawRect(41, 3, 11, 19);
					g.setColor(Color.green);
					g.fillRect(42, 4, 10, 18);
				}
			}
		}
		else if(location == "east")
		{
			if(numHotels == 1)
			{
				g.setColor(Color.black);
				g.drawRect(3, 15, 19, 24);
				g.setColor(Color.red);
				g.fillRect(4, 16, 18, 23);
			}
			else if(numHotels == 0)
			{
				if(numHouses >= 1)
				{
					g.setColor(Color.black);
					g.drawRect(3, 41, 19, 11);
					g.setColor(Color.green);
					g.fillRect(4, 42, 18, 10);
				}
				if(numHouses >= 2)
				{
					g.setColor(Color.black);
					g.drawRect(3, 28, 19, 11);
					g.setColor(Color.green);
					g.fillRect(4, 29, 18, 10);
				}
				if(numHouses >= 3)
				{
					g.setColor(Color.black);
					g.drawRect(3, 15, 19, 11);
					g.setColor(Color.green);
					g.fillRect(4, 16, 18, 10);
				}
				if(numHouses == 4)
				{
					g.setColor(Color.black);
					g.drawRect(3, 2, 19, 11);
					g.setColor(Color.green);
					g.fillRect(4, 3, 18, 10);
				}
			}
		}
		else if(location == "west")
		{
			if(numHotels == 1)
			{
				g.setColor(Color.black);
				g.drawRect(77, 15, 19, 24);
				g.setColor(Color.red);
				g.fillRect(78, 16, 18, 23);
			}
			else if(numHotels == 0)
			{
				if(numHouses >= 1)
				{
					g.setColor(Color.black);
					g.drawRect(77, 2, 19, 11);
					g.setColor(Color.green);
					g.fillRect(78, 3, 18, 10);
				}
				if(numHouses >= 2)
				{
					g.setColor(Color.black);
					g.drawRect(77, 15, 19, 11);
					g.setColor(Color.green);
					g.fillRect(78, 16, 18, 10);
				}
				if(numHouses >= 3)
				{
					g.setColor(Color.black);
					g.drawRect(77, 28, 19, 11);
					g.setColor(Color.green);
					g.fillRect(78, 29, 18, 10);
				}
				if(numHouses == 4)
				{
					g.setColor(Color.black);
					g.drawRect(77, 41, 19, 11);
					g.setColor(Color.green);
					g.fillRect(78, 42, 18, 10);
				}
			}
		}
	} // end paintComponent
	
	private class PropertyListener implements MouseListener
	{
		public void mouseClicked(MouseEvent event)
		{			
			// open a frame with the PropertyCardPanel
			JFrame frame = new JFrame ("Property Card");
			
			frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
			
//			JPanel j = new JPanel();
			
			// add the PropertyCardPanel to the frame
			frame.getContentPane().add(pcp);
			frame.pack();
			frame.setLocationRelativeTo(null);
			frame.setResizable(true);
			frame.setVisible(true);
			
		}
		
		public void mousePressed(MouseEvent event){}
		public void mouseReleased(MouseEvent event){}
		public void mouseEntered(MouseEvent event)
		{
//			mouseFlag = true;
//			repaint();
		}
		public void mouseExited(MouseEvent event)
		{
//			mouseFlag = false;
//			repaint();
		}		
	} // end class PropertyListener
	
	
	public void updateBoard()
	{
		repaint();
	}
	 
	
	
} // end class PropertyLot
